#!/usr/local/bin/perl -w

#
#    Zhejiang TianKe
#    Author: SHANG
#    version 0.1
#    date: 2012-12-04
#    License: GPL
#
#    Summary: 
#    this program combined the data process to clean the data and basic 
#    read file infomation generated.
#
use strict;
use warnings;

if(@ARGV<2){
  print "usage:<dir><SCRIPT_ROOT>\n";
  exit;
}
my $dir=shift;
my $script_root = shift;

opendir(DIR, $dir) || die "can't open the $dir\n";
my @file=readdir(DIR);
closedir DIR;

foreach my $file ( @file )
{
   if($file=~m/fq$/ )
   {
   
   #run_S01_fq_process($script_root, $dir, $file);
   #run_S02_runFastQC ($script_root, $dir, $file);
   #run_S03_read_FastQC_INFO($script_root, $dir, $file);
   }
}

   run_C04_Merge_basic_Info($dir);

#S01 filter trim the fastq file
sub run_S01_fq_process {
   my ($script_root,$dir,$file)=@_;
   my $command="perl $script_root/S01_fq_process.pl $dir/$file";
   system($command);
}

#S02 evaluate the fastq with the fastQC
sub run_S02_runFastQC{

   my ($script_root,$dir,$file)=@_;
   my $command="perl $script_root/S02_runFastQC.pl $dir/$file";
   system($command);
}

#S03 extract the basic fastQC data
sub run_S03_read_FastQC_INFO{

   my ($script_root,$dir,$input_file)=@_;
   my $command="perl $script_root/S03_read_FastQC_INFO.pl "."$dir/$input_file"."_fastQC_dir/".$input_file."_fastqc ".$input_file.".basic_Info.dat";  
   #print "debug: ".$command."\n";
   system($command);
}

#C00 re-construct the basic info 
sub run_C04_Merge_basic_Info{

   my ($dir)=@_;
   my $rm_command="rm -rf basic_info_output/";
   system($rm_command) if (-e "basic_info_output/*");
   mkdir "basic_info_output";
   my $command="mv *.basic_Info.dat $dir/basic_info_output/";
   system($command);
   my $cat_command="cat $dir/basic_info_output/*.basic_Info.dat>Total_basic_Info.txt";
   system($cat_command);
  
}


